
#include <iostream>
#include <fstream>
#include <sstream>
#include "MarbleGameSolver.h"


int main(int argc, char *argv[])
{
    if( argc <  2 )
    {
        std::cout << "please pass tasks file name" << std::endl;
        return 1;
    }

    std::ifstream stream( argv[1], std::ifstream::in );

    if( stream.bad() )
    {
        std::cout << "please pass valid file name" << std::endl;
        return 1;
    }

    MarbleGameSolver solver(4);     // Gonna spawn 4 extra threads

    char linebuf[512];
    memset( linebuf, 0, 512 );

    std::string task;

    int fieldSize = 0;
    int marbleNo = 0;
    int wallsNo = 0;
    int extra = 0;

    int taskCounter = -1;

    while( stream.good() )
    {
        stream.getline( linebuf,  512 );

        std::istringstream line( linebuf );

        if( ( line >> fieldSize >> marbleNo >> wallsNo ) )
        {
            if( !( line >> extra ) )
            {
                // Three integers in a line mean beginning of task
                taskCounter++;

                if( taskCounter > 0 )
                {
                    std::string res = solver.findShortestSolution( task );

                    int resLength = res.length();

                    if( resLength > 0 )
                    {
                        std::cout   << "Case " << taskCounter << ": " << resLength
                                    << " moves " << res << std::endl;
                    }
                    else
                    {
                        std::cout << "Case " << taskCounter << ": impossible" << std::endl;
                    }

                    res.clear();
                    task.clear();
                }

                if( fieldSize == 0 )
                {
                    // This means end of tasks
                    // three 0's
                    break;
                }
            }
        }

        task += linebuf;
        task += " ";
    }

    stream.close();
    return 0;
}
